class Bus {
  constructor() {
    this.list = {}; // 收集存放事件,相当于收集订阅信息,调度中心
  }
  // 订阅
  $on(name, callback) {
    const fn = this.list[name] || [];
    fn.push(callback);
    this.list[name] = fn;
  }
  // 发布
  $emit(name, ...args) {
    const eventName = this.list[name];
    eventName.forEach((fn) => {
      fn.apply(this, args);
    });
  }
  // 取消
  $off(name) {
    if (name) {
      if (this.list[name]) {
        delete this.list[name];
      }
    } else {
      this.list = {}
    }
  }
}
// 导出Bus的实例
export default new Bus();